from marshmallow_sqlalchemy import ModelSchema
from marshmallow import fields
from application.api.users.models import *
from application import db


class UserSchema(ModelSchema):
    class Meta(ModelSchema.Meta):
        model = Users
        sqla_session = db.session

    id = fields.Integer(dump_only=True)
    username = fields.String(required=True, validate=lambda s: 128 > len(s.strip()) > 0, error_messages={'required': '名称必填', 'validator_failed': '名称小于128位且不能为空'})
    password = fields.String(load_only=True, validate=lambda s: 256 > len(s.strip()) > 0, error_messages={'required': '名称必填', 'validator_failed': '密码小于256位且不能为空'})
    mobile = fields.String(required=True, validate=lambda s: 32 > len(s.strip()) > 0, error_messages={'required': '手机号类型必填', 'validator_failed': '手机号小于32位且不能为空'})
    user_type = fields.Integer(required=True, validate=lambda s: s in [1, 2, 3], error_messages={'required': '用户类型必填', 'validator_failed': '用户类型错误'})


class WorkSchema(ModelSchema):
    class Meta(ModelSchema.Meta):
        model = WorkDetail
        sqla_session = db.session

    id = fields.Integer(dump_only=True)
    name = fields.String(required=True, validate=lambda s: 128 > len(s.strip()) > 0,error_messages={'required': '名称必填', 'validator_failed': '名称小于128位且不能为空'})
    score = fields.Integer(validate=lambda s:11 > s > -1, error_messages={'validator_failed': '分数范围0到10'})
    rater = fields.String(validate=lambda s: len(s.strip()) > 0, error_messages={'validator_failed': '评委不能为空'})
    upload_time = fields.DateTime()



class RotationSchema(ModelSchema):
    class Meta(ModelSchema.Meta):
        model = Rotation
        sqla_session = db.session

    id = fields.Integer(dump_only=True)
    first_round = fields.Integer(validate=lambda s: 11 > s > -1, error_messages={'validator_failed': '分数范围0到10'})
    second_round = fields.Integer(validate=lambda s: 11 > s > -1, error_messages={'validator_failed': '分数范围0到10'})
    final_round = fields.Integer(validate=lambda s: 11 > s > -1, error_messages={'validator_failed': '分数范围0到10'})